package com.demo.web.pc;

import com.demo.common.Constants;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.SalePerson;
import com.demo.entity.Secretary;
import com.demo.service.ISecretaryService;
import com.demo.vo.SecretaryVo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/secretary")
public class SecretaryPcController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISecretaryService depService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Secretary> get(Secretary arg) {
        Secretary service_res = depService.selectByPrimaryKey(arg.getId());
        BaseResult<Secretary> res = new BaseResult<Secretary>(true, service_res);
        return res;
    }

    /***
     * 创建业务
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> create(Secretary arg, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute("current");
        BaseResult<Object> res = null;
        if (sp != null) {
            arg.setNo(Constants.generateServiceNO("S"));
            arg.setSaleId(String.valueOf(sp.getId()));
            arg.setSaleName(sp.getUserName());
            int service_res = depService.insertSelective(arg);
            res = new BaseResult(true, service_res);
        } else {
            res = new BaseResult(false, "请登录", 401);
        }
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<Object> query(SecretaryVo arg, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute("current");
        ListResult<Object> res = null;
        if (sp != null) {
            arg.setSaleId(String.valueOf(sp.getId()));
            List<Secretary> list = depService.queryByPage(arg);
            int total = depService.queryCount(arg);
            Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
            res = new ListResult(true, list);
            res.setPage(page);
        } else {
            res = new ListResult(false, "请登录", 401);
        }

        return res;
    }
}
